<?php

namespace App\Http\Controllers\Api\Merchant;

use App\Coupons;
use App\Http\Services\CommonManager;
use App\Http\Services\MerchantManager;
use App\Http\Services\UserManager;
use App\Merchant;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CouponController extends Controller
{
    /**
     * 获取优惠券列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCouponList(Request $request){
        /** @var User $merchantUser */
        $merchantUser = MerchantManager::isLogin($request);
        if(!$merchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($merchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $limit      = !is_null($request->get('limit')) ? $request->get('limit') : 20;
        $page       = !is_null($request->get('page')) ? $request->get('page') : 1;
        $merchantId = $request->get('merchant_id',0);

        $pageInfo = [
            'limit' => $limit,
            'page'  => $page
        ];

        /** @var Coupons $coupon */
        $coupons = Coupons::whereEnterpriseId($enterpriseInfo->id);

        if($merchantId != 0){
            $coupons->whereMerchantId($merchantId);
        }

        $pageInfo['total'] = $coupons->count();

        $couponIds = $coupons->orderBy('created_at','desc')->offset($limit * ($page - 1))->limit($limit)->pluck('id')->toArray();

        $couponList = CommonManager::getCouponToId($couponIds);

        return response()->json(['level' => 'success','message' => '获取成功','data' => $couponList,'page_info' => $pageInfo]);
    }

    /**
     * 获取优惠券详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCouponDetail(Request $request){
        /** @var User $merchantUser */
        $merchantUser = MerchantManager::isLogin($request);
        if(!$merchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $coupon = CommonManager::getCouponToId($id);

        return response()->json(['level' => 'error','message' => '获取成功','data' => $coupon]);
    }

    /**
     * 设置优惠券
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setCoupon(Request $request){
        /** @var User $merchantUser */
        $merchantUser = MerchantManager::isLogin($request);
        if(!$merchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $enterpriseInfo = MerchantManager::getMerchantInfoByUserId($merchantUser->id);
        if(!$enterpriseInfo)
            return response()->json(['level' => 'error','message' => '商家信息异常']);

        $remark         = !is_null($request->get('remark')) ? $request->get('remark') : '';
        $name           = !is_null($request->get('name')) ? $request->get('name') : '';
        $money          = !is_null($request->get('money')) ? $request->get('money') : 0;
        $startTime      = !is_null($request->get('start_time')) ? $request->get('start_time') : '';
        $endTime        = !is_null($request->get('end_time')) ? $request->get('end_time') : '';
        $type           = !is_null($request->get('type')) ? $request->get('type') : '';
        $satisfyMoney   = !is_null($request->get('satisfy_money')) ? $request->get('satisfy_money') : 0;
        $num            = !is_null($request->get('num')) ? $request->get('num') : 0;
        $status         = !is_null($request->get('status')) ? $request->get('status') : 1;
        $imgId          = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $id             = !is_null($request->get('id')) ? $request->get('id') : 0;
        $merchantId = $request->get('merchant_id',0);

        $merchant = Merchant::whereId($merchantId)->first();
        if(!$merchant)
            return response()->json(['level' => 'error','message' => '请选择门店']);

        $newCoupon = Coupons::whereId($id)->first();
        if(!$newCoupon)
            $newCoupon = new Coupons();

        $newCoupon->enterprise_id               = $enterpriseInfo->id;
        $newCoupon->merchant_id                 = $merchantId;
        $newCoupon->name                        = $name;
        $newCoupon->money                       = $money;
        $newCoupon->start_time                  = $startTime;
        $newCoupon->end_time                    = $endTime;
        $newCoupon->type                        = $type;
        $newCoupon->satisfy_money               = $satisfyMoney;
        $newCoupon->num                         = $num;
        $newCoupon->remark                      = $remark;
        $newCoupon->img_id                      = $imgId;
        $newCoupon->status                      = $status;
        $newCoupon->save();

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newCoupon]);
    }

    /**
     * 删除优惠券
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delCoupon(Request $request){
        /** @var User $merchantUser */
        $merchantUser = MerchantManager::isLogin($request);
        if(!$merchantUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $coupon = Coupons::whereId($id)->first();
        if(!$coupon)
            return response()->json(['level' => 'error','message' => '该优惠券不存在']);

        $coupon->delete();

        return response()->json(['level' => 'success','message' => '获取成功','data' => $coupon]);
    }


}
